package com.example.mobifin_busticket.views.activities;

import java.util.ArrayList;
import java.util.List;

import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.R.id;
import com.example.mobifin_busticket.R.layout;
import com.example.mobifin_busticket.R.menu;
import com.example.mobifin_busticket.controller.databasemanager.DatabaseManager;
import com.example.mobifin_busticket.models.BusModel;
import com.example.mobifin_busticket.models.SourceModel;
import com.example.mobifin_busticket.views.adapter.CustomListAdapter;
import com.example.mobifin_busticket.views.adapter.CustomListAdapterForTrips;
import com.example.mobifin_busticket.views.adapter.FilterBusAdapter;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class FilterBusByProperty extends MobifinActivity implements
		android.view.View.OnClickListener {
	TextView tvHeader, tvSearch;
	FilterBusAdapter filterListAdapter;
	ListView list;
	Button btn_done, btn_clr_all;
	private String strDynamicvalue = null;
	private List<String> listNameByProperty = null;
	private List<String> selectedListonDoneClick = null;
	private List<String> selectListObject = null;
	LinearLayout llslct;
	String strDynamicValue;
	CheckBox chkselectall = null;
	CheckBox chkbox = null;
	DatabaseManager dm;
	List<String> dynamicList;
	int position;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filter_bus_by_property);
		tvHeader = (TextView) findViewById(R.id.txt_dyanamicName);
		tvSearch = (TextView) findViewById(R.id.txt_search);
		list = (ListView) findViewById(R.id.lst_details);
		chkselectall = (CheckBox) findViewById(R.id.chkBox);
		llslct = (LinearLayout) findViewById(R.id.ll_slct);
		btn_done = (Button) findViewById(R.id.btn_done_in_proprty);
		btn_clr_all = (Button) findViewById(R.id.btn_clr_in_property);
		tvSearch.setOnClickListener(this);
		btn_done.setOnClickListener(this);
		btn_clr_all.setOnClickListener(this);
		strDynamicValue = getIntent().getStringExtra("dyanamicValue");
		tvHeader.setText(strDynamicValue);

		listNameByProperty = mDisplayDynamicValue();

		filterListAdapter = new FilterBusAdapter(FilterBusByProperty.this,
				listNameByProperty, chkselectall);
		list.setAdapter(filterListAdapter);

		llslct.setOnClickListener(this);
		tvSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				filterListAdapter.ItemFilter(s.toString());

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
	}

	private List<String> mDisplayDynamicValue() {
		// TODO Auto-generated method stubdm = new

		dm = new DatabaseManager(FilterBusByProperty.this);
		dynamicList = new ArrayList<String>();
		if (strDynamicValue.equals(getResources().getString(
				R.string.filter_by_travels))) {

			tvSearch.setHint(getResources().getString(
					R.string.enter_filter_travel_hint));
			if (dynamicList != null) {
				dynamicList.clear();
			}
			if (dm.getTravelsName().isEmpty()) {
				chkselectall.setChecked(false);
			}
			dynamicList = dm.getTravelsName();
		} else if (strDynamicValue.equals(getResources().getString(
				R.string.filter_by_boarding))) {

			tvSearch.setHint(getResources().getString(
					R.string.enter_filter_boarding_hint));
			if (dynamicList != null) {
				dynamicList.clear();
			}
			if (dm.getBoardingName().isEmpty()) {
				chkselectall.setChecked(false);
			}
			dynamicList = dm.getBoardingName();
		} else if (strDynamicValue.equals(getResources().getString(
				R.string.filter_by_droping))) {

			tvSearch.setHint(getResources().getString(
					R.string.enter_filter_droping_hint));
			if (dynamicList != null) {
				dynamicList.clear();
			}
			if (dm.getBoardingName().isEmpty()) {
				chkselectall.setChecked(false);
			}
			dynamicList = dm.getDropingName();
		}
		dm.close();
		return dynamicList;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_done_in_proprty:
			OnClickDone();
			break;
		case R.id.btn_clr_in_property:
			OnClickResetAll();
			break;
		case R.id.ll_slct:
			OnClickSelectAll();
			break;
		default:
			break;
		}
	}

	private void OnClickSelectAll() {
		// TODO Auto-generated method stub
		if (filterListAdapter != null) {
			filterListAdapter.selectedAll();
			chkselectall.setChecked(true);
			System.out.println("on click select all");
		}
	}

	public void OnClickDone() {
		selectedListonDoneClick = new ArrayList<String>();
		selectedListonDoneClick = filterListAdapter.getFilterSelectedList();

		Intent ListIntent = new Intent();
		ListIntent.putStringArrayListExtra("FilterByProperty",
				(ArrayList<String>) selectedListonDoneClick);
		setResult(RESULT_OK, ListIntent);
		finish();

		System.out.println("new list:" + selectedListonDoneClick);
	}

	private void OnClickResetAll() {
		// TODO Auto-generated method stub
		if (filterListAdapter != null) {
			chkselectall.setChecked(false);
			filterListAdapter.uncheckedAll();
			System.out.println("on click uncheck all");
		}
		
		filterListAdapter.notifyDataSetChanged();

	}
}
